package com.leo;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/**
 * @ClassName ListViewDemo
 * @Description 五种数据类型得增删改
 * @Author wangss
 * @date 2020.06.09 11:04
 * @Version 1.0
 */
public class ListViewDemo7 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(an);

        ObservableList<PersonProperty2> callList = FXCollections.observableArrayList(new Callback<PersonProperty2, Observable[]>() {
            @Override
            public Observable[] call(PersonProperty2 param) {
                SimpleStringProperty[] properties = new SimpleStringProperty[]{param.ageProperty()};
                return properties;
            }
        });

        ListView<PersonProperty2> listView = new ListView<>(callList);
        listView.setPlaceholder(new Label("没有数据"));
        listView.setPrefWidth(300d);
        listView.setPrefWidth(300d);

        ObservableList<PersonProperty2> list = listView.getItems();
        list.add(new PersonProperty2("A", "1"));
        list.add(new PersonProperty2("B", "2"));
        list.add(new PersonProperty2("C", "3"));
        list.add(new PersonProperty2("D", "4"));

        Button button = new Button("button");

        an.getChildren().addAll(button, listView);

        AnchorPane.setTopAnchor(listView, 50d);
        AnchorPane.setLeftAnchor(listView, 100d);

        primaryStage.setScene(scene);
        primaryStage.setTitle("ListView");
        primaryStage.setWidth(600d);
        primaryStage.setHeight(600d);
        primaryStage.setResizable(false);
        primaryStage.show();

        listView.setCellFactory(TextFieldListCell.forListView(new StringConverter<PersonProperty2>() {
            @Override
            public String toString(PersonProperty2 object) {
                return object.getName() + " - " + object.getAge();
            }

            @Override
            public PersonProperty2 fromString(String string) {
                return null;
            }
        }));

        button.setOnAction(event -> {

            // 这里 age 为 simple类，name为string，所以要把age放到最后更新，这样更新时界面会正常显示
            list.get(0).setName("wangss");
            list.get(0).setAge("123");

        });

        list.addListener(new ListChangeListener<PersonProperty2>() {
            @Override
            public void onChanged(Change<? extends PersonProperty2> c) {
                while (c.next()) {
                    if (c.wasAdded()) {
                        System.out.println("添加操作");
                    } else if (c.wasUpdated()) {
                        System.out.println("更新操作");
                    } else if (c.wasRemoved()) {
                        System.out.println("移除操作");
                    } else if (c.wasReplaced()) {
                        System.out.println("替换操作");
                    }
                }
            }
        });
    }
}
